package com.tubruk.database;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.location.Location;

import com.tubruk.object.TrackSummary;
import com.tubruk.trackme.MyGeoPoint;

public class TrackSummaryDB {
	
	public static List<TrackSummary> getAll(){
		List<TrackSummary> result = null;

		DatabaseHelper db = null;

		try {
			db = new DatabaseHelper();
			Cursor c = db.query(DatabaseHelper.TABLE_MAPSUMMARY.TABLENAME, null, null, null, "_id asc");

			if (c != null){
				result = new ArrayList<TrackSummary>();
				if (c.moveToFirst()){
					do {
						result.add(convertDBToTrackSummary(c));
					}while (c.moveToNext());
				}
			}
		}finally {
			if (db != null){
				db.destroy();
			}
		}
		return result;
	}
	
	private static TrackSummary convertDBToTrackSummary(Cursor c){
		TrackSummary result = new TrackSummary();
		
		result.setId(c.getInt(c.getColumnIndex( DatabaseHelper.TABLE_MAPSUMMARY.ID)));
		result.setAvgSpeed(c.getFloat(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.SPEEDAVG)));
		result.setDatatype(c.getInt(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.DATATYPE)));
		result.setDistance(c.getDouble(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.DISTANCE)));
		result.setMaxSpeed(c.getFloat(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.SPEEDMAX)));
		result.setMinSpeed(c.getFloat(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.SPEEDMIN)));
		result.setMode(c.getInt(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.MODE)));
		result.setTimeSpent(c.getLong(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.TIMESPENT)));
		result.setTimeStart(c.getLong(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.TIMESTART)));
		result.setTimeStop(c.getLong(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.TIMESTOP)));
		result.setTitle(c.getString(c.getColumnIndex(DatabaseHelper.TABLE_MAPSUMMARY.TITLE)));
			
		return result;
	}
	
	private static MyGeoPoint convertDBToTrackDetail(Cursor c){
		Location loc = new Location("");
		
		loc.setAccuracy(c.getFloat(c.getColumnIndex(DatabaseHelper.TABLE_MAPDETAIL.ACCURACY)));
		loc.setAltitude(c.getDouble(c.getColumnIndex(DatabaseHelper.TABLE_MAPDETAIL.ALTITUDE)));
		loc.setBearing(c.getFloat(c.getColumnIndex(DatabaseHelper.TABLE_MAPDETAIL.BEARING)));
		loc.setLatitude(c.getDouble(c.getColumnIndex(DatabaseHelper.TABLE_MAPDETAIL.LATITUDE)));
		loc.setLongitude(c.getDouble(c.getColumnIndex(DatabaseHelper.TABLE_MAPDETAIL.LONGITUDE)));
		loc.setProvider(c.getString(c.getColumnIndex(DatabaseHelper.TABLE_MAPDETAIL.PROVIDER)));
		loc.setSpeed(c.getFloat(c.getColumnIndex(DatabaseHelper.TABLE_MAPDETAIL.SPEED)));
		loc.setTime(c.getLong(c.getColumnIndex(DatabaseHelper.TABLE_MAPDETAIL.TIME)));
		
		MyGeoPoint result = new MyGeoPoint(loc);
		return result;
	}
	
	public static void getTrackSummaryDetails(TrackSummary sum){
		DatabaseHelper db = null;

		List<MyGeoPoint> points = sum.getGeoPoints();
		if (points != null){
			points.clear();
		}else {
			points = new ArrayList<MyGeoPoint>();
			sum.setGeoPoints(points);
		}
		
		try {
			db = new DatabaseHelper();
			Cursor c = db.query(DatabaseHelper.TABLE_MAPDETAIL.TABLENAME, null, DatabaseHelper.TABLE_MAPDETAIL.SUMMARY_ID + "=" + sum.getId(), null, "_id asc");

			if (c != null){
				if (c.moveToFirst()){
					do {
						points.add(convertDBToTrackDetail(c));
					}while (c.moveToNext());
				}
			}
		}finally {
			if (db != null){
				db.destroy();
			}
		}
	}
	
	public static int delete(long id){
		int result = -1;
		
		DatabaseHelper dbHelper = null;
		
		try {
			dbHelper = new DatabaseHelper();
			
			//mestinya transaction, tapi sudahlah :D
			dbHelper.delete(DatabaseHelper.TABLE_MAPDETAIL.TABLENAME, DatabaseHelper.TABLE_MAPDETAIL.SUMMARY_ID + "=" + id, null);
			result = dbHelper.delete(DatabaseHelper.TABLE_MAPSUMMARY.TABLENAME, DatabaseHelper.TABLE_MAPSUMMARY.ID + "=" + id, null);
			
		}finally{
			if (dbHelper != null){
				dbHelper.destroy();
			}
		}
		
		
		return result;
	}
	
	public static int update(TrackSummary sum){
		int result = -1;
		DatabaseHelper dbHelper = null;
		
		try {
			dbHelper = new DatabaseHelper();
			
			ContentValues values = new ContentValues();
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.TITLE, sum.getTitle());
			result = dbHelper.update(DatabaseHelper.TABLE_MAPSUMMARY.TABLENAME, values, "_id=" + sum.getId(), null);
		}finally{
			if (dbHelper != null){
				dbHelper.destroy();
			}
		}
		
		return result;
	}

	public static long save(TrackSummary sum){
		long result = -1;
		
		DatabaseHelper dbHelper = null;
		
		try {
			dbHelper = new DatabaseHelper();
			
			ContentValues values = new ContentValues();
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.DATATYPE, sum.getDataType());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.DISTANCE,  sum.getDistance());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.MODE, sum.getMode());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.SPEEDAVG, sum.getAvgSpeed());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.SPEEDMIN, sum.getMinSpeed());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.SPEEDMAX, sum.getMaxSpeed());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.TIMESPENT, sum.getTimeSpent());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.TIMESTART, sum.getTimeStart());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.TIMESTOP, sum.getTimeStop());
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.TITLE, sum.getTitle());
			
			long time = new Date().getTime();
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.DATECREATED, time);
			values.put(DatabaseHelper.TABLE_MAPSUMMARY.DATEMODIFIED, time);
			
			result = dbHelper.insert(DatabaseHelper.TABLE_MAPSUMMARY.TABLENAME, values);
			if (result != -1){
				sum.setId(result);
				
				//save details
				List<MyGeoPoint> myGeoPoints = sum.getGeoPoints();
				int size = myGeoPoints.size();
				for (int i=0;i<size;i++) {
					MyGeoPoint myg = myGeoPoints.get(i);
					
					values.clear();
					Location loc = myg.getLocation();
					values.put(DatabaseHelper.TABLE_MAPDETAIL.ACCURACY, loc.getAccuracy());
					values.put(DatabaseHelper.TABLE_MAPDETAIL.ALTITUDE, loc.getAltitude());
					values.put(DatabaseHelper.TABLE_MAPDETAIL.BEARING, loc.getBearing());
					values.put(DatabaseHelper.TABLE_MAPDETAIL.LATITUDE, loc.getLatitude());
					values.put(DatabaseHelper.TABLE_MAPDETAIL.LONGITUDE, loc.getLongitude());
					values.put(DatabaseHelper.TABLE_MAPDETAIL.PROVIDER, loc.getProvider());
					values.put(DatabaseHelper.TABLE_MAPDETAIL.SPEED, loc.getSpeed());
					values.put(DatabaseHelper.TABLE_MAPDETAIL.SUMMARY_ID, result);
					values.put(DatabaseHelper.TABLE_MAPDETAIL.TIME, loc.getTime());
					
					if (dbHelper.insert(DatabaseHelper.TABLE_MAPDETAIL.TABLENAME, values) < 0) {
						result = -1;
						i = size;
					}
				}	
				
			}
		}finally{
			if (dbHelper != null){
				dbHelper.destroy();
			}
		}
		
		return result;
	}
}
